//
//  BeginOpenViewController.m
//  Xspcf
//
//  Created by 小小算盘 on 16/2/17.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "BeginOpenViewController.h"
#import "SunTabController.h"
#import "Masonry.h"
#import "XSPPageControl.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
@interface BeginOpenViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)XSPPageControl *pageControl;
@property (nonatomic,strong)UIButton *btn;
@end

@implementation BeginOpenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCollectionView];
}

-(void)setupCollectionView{
    // 首次打开 清理登录信息
    [Sun setUser:@"0" forKey:KLOGIN];
    [Sun setUser:nil forKey:USER];
    [Sun setUser:nil forKey:MD5];
    [Sun setUser:nil forKey:DES];
    [Sun setUser:nil forKey:ACCESSID];

    
    self.dataArr = [[NSMutableArray alloc]initWithObjects:@"guide_1",@"guide_2",@"guide_3", nil];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    self.scrollView.contentSize = CGSizeMake(SCREENWIDTH*3, SCREENHEIGHT);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    for ( int i= 0; i<3; i++) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, SCREENHEIGHT)];
        img.image = [UIImage imageNamed:self.dataArr[i]];
        [self.scrollView addSubview:img];
        if (i == 5) {
            UIButton *btn = [[UIButton alloc]init];
            [img addSubview:btn];
            [btn lt_setBottom:Y(60) to:nil save:YES];
            [btn lt_XcenterTo:nil save:YES];
            [btn lt_setSize:CGSizeMake(X(300), Y(60)) save:YES];
            [btn addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
            img.userInteractionEnabled = YES;
        }
    }
    [self.view addSubview:self.scrollView];
    
    
    self.pageControl = [[XSPPageControl alloc] init];
//    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
    [self.pageControl addTarget:self action:@selector(changePage:)forControlEvents:UIControlEventValueChanged];
    [self.pageControl setValue:[UIImage imageNamed:@"guide_selete"] forKeyPath:@"_currentPageImage"];
    [self.pageControl setValue:[UIImage imageNamed:@"guide_normal"] forKeyPath:@"_pageImage"];
    
    [self.view addSubview:self.pageControl];
//    [self.pageControl lt_XcenterTo:nil save:YES];
    [self.pageControl lt_setRight:X(0) to:nil save:YES];
    [self.pageControl lt_setBottom:Y(15) to:nil save:YES];
    UIButton *btn1 = [[UIButton alloc]init];
    [self.view addSubview:btn1];
    [btn1 lt_Bottom:SunEdgesMake(X(90), Y(44), X(90), Y(37)) to:nil save:YES];
    btn1.clipsToBounds = YES;
    [btn1 setBackgroundImage:[UIImage imageNamed:@"guide_btn"] forState:UIControlStateNormal];
    btn1.layer.cornerRadius = 4;
//    btn1.layer.borderWidth = 1;
//    btn1.layer.borderColor = [HXColor colorWithHexString:@"FECB16"].CGColor;
//    [btn1 setTitle:@"注册领588元红包" forState:UIControlStateNormal];
//    [btn1 setTitleColor:WHITE forState:UIControlStateNormal];
//    [btn1 setTitleColor:[HXColor colorWithHexString:@"FECB16"] forState:UIControlStateHighlighted];
//    btn1.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn1 addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
//    UIButton *btn2 = [[UIButton alloc]init];
//    [self.view addSubview:btn2];
//    [btn2 lt_Top:SunEdgesMake(Y(8), X(100), X(100), Y(31)) to:btn1 save:YES];
//    btn2.clipsToBounds = YES;
//    [btn2 setBackgroundColor:WHITE forState:UIControlStateNormal];
//    [btn2 setBackgroundColor:[HXColor colorWithHexString:@"7CD8D7"] forState:UIControlStateHighlighted];
//    btn2.layer.cornerRadius = 4;
//    btn2.layer.borderWidth = 1;
//    btn2.layer.borderColor = [HXColor colorWithHexString:@"7CD8D7"].CGColor;
//    [btn2 setTitle:@"登录" forState:UIControlStateNormal];
//    [btn2 setTitleColor:[HXColor colorWithHexString:@"7CD8D7"] forState:UIControlStateNormal];
//    [btn2 setTitleColor:WHITE forState:UIControlStateHighlighted];
//    btn2.titleLabel.font = [UIFont systemFontOfSize:13];
//    [btn2 addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn3 = [[UIButton alloc]init];
    [self.view addSubview:btn3];
    [btn3 lt_Top:SunEdgesMake(Y(8), X(100), X(100), Y(11)) to:btn1 save:YES];
    NSMutableAttributedString *detailString = [[NSMutableAttributedString alloc]initWithString:@"立即体验>"];
    NSRange strRange = {0,[detailString length]};
    [detailString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [detailString addAttribute:NSForegroundColorAttributeName value:[HXColor colorWithHexString:@"ab56e6"] range:strRange];
    [btn3 setAttributedTitle:detailString forState:UIControlStateNormal];

    btn3.titleLabel.font = [UIFont systemFontOfSize:16 weight:1];
    [btn3 addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
    
    
//    self.btn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenW - 70, 35, 50, 24)];
//    [self.view addSubview:self.btn];
//    self.btn.layer.cornerRadius = 3;
//    self.btn.layer.borderWidth = 1;
//    self.btn.hidden = YES;
//    self.btn.layer.borderColor = [UIColor whiteColor].CGColor;
//    [self.btn setTitle:@"跳过" forState:UIControlStateNormal];
//    self.btn.titleLabel.font = [UIFont systemFontOfSize:14];
//    [self.btn addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
}

- (void)login{
    [self enter];
    [SunSupport shouldPushController:@{@"action":@"3"}];
    [Sun setUser:@"1" forKey:NOALERT];
}

- (void)enter{
#if 0
    //模态跳转到 tabBar
    ZHBTabBarController *picket = [[ZHBTabBarController alloc] init];
    [picket setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:picket animated:YES completion:nil];
#else
    //把window的跟视图控制器 换成PicketKitchenTabBarController
    id temp = [UIApplication sharedApplication].delegate;
    AppDelegate *delegate = temp;
    delegate.tab = [[SunTabController alloc] init];
    //获取window
    //UIWindow *window = self.view.window;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    window.rootViewController = delegate.tab;
#endif
}

#pragma mark - 协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x>KScreenW*2) {
        [self enter];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.pageControl.currentPage = self.scrollView.contentOffset.x/SCREENWIDTH;
}

- (void)changePage:(id)sender {
    int page = (int)self.pageControl.currentPage;
    [self.scrollView setContentOffset:CGPointMake(SCREENWIDTH * page, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
